package com.jww.dao;


import com.jww.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentTest {

    //   基础完整版
    @Test
    public void find() throws Exception {

        //1 创建业务对象
        InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
        //2 创建sqlSessionFaction工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3 获取sqlSession对象
        SqlSession session = factory.openSession();
        //4 执行对应操作
        //selectList:参数  namespace+"."+statement对象ID
        List<Student> list = session.selectList("StudentMapper.findAll");
        System.out.println(list);
        //5.事务提交:更新      
        session.commit();
        //6.释放资源    
        session.close();
    }


    private SqlSession session;
    private InputStream is;

    @Before
    public void before() throws IOException {
        //1 创建业务对象
        is = Resources.getResourceAsStream("mybatisConfig.xml");
        //2 创建sqlSessionFaction工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3 获取sqlSession对象
        session = factory.openSession();
    }

    @After
    public void after() throws IOException {
        //5.事务提交:更新      
        session.commit();
        //6.释放资源    
        session.close();
        is.close();
    }

    @Test
    //新增操作
    public void saveStudent() throws ParseException {
        Student stu = new Student();
        stu.setAge(18);
        stu.setName("三毛");
        String s = "1997-11-02";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf.parse(s);
        stu.setDate(parse);
        System.out.println("执行保存前：" + stu);
        session.insert("StudentMapper.saveStudent", stu);
        System.out.println("执行保存后：" + stu);

    }


    //模糊查询
    @Test
    public void findLikeName() {
        List<Student> list = session.selectList("StudentMapper.findLikeNamec-", "%阳%");
        System.out.println("list = " + list);
    }


    //通过姓名生日进行查询
    @Test
    public void findByNameAndBirthday() {
        HashMap map = new HashMap();
        map.put("name1", "birthday1");
        map.put("name2", "birthday2");
        Student stu = session.selectOne("StudentMapper.findByNameAndBirthday", map);
        System.out.println("stu = " + stu);
    }

    //修改测试
    @Test
    public void updateById() {
//        Student s =new Student(1,"1111",15,new Date());
                Student stu = session.selectOne("StudentMapper.findById", 2);
        stu.setName("习大大");
        int update = session.update("StudentMapper.updateById", stu);
        System.out.println("update = " + update);
    }

    //-----------------------------------------------------

    //name分组查询
    @Test
    public void findNameCount() {
        List<Map> map = session.selectList("StudentMapper.findNameCount");
        System.out.println(map);
        //重装封装我们所需要的数据
        HashMap nameMap = new HashMap();
        for (Map map1 : map) {
            String name = (String) map1.get("name");
            Long total = (Long) map1.get("count(*)");
            nameMap.put(name, total);
        }
        System.out.println(nameMap);

    }


    //测试统计人员数量
    @Test
    public void findCount() {
        Integer i = session.selectOne("StudentMapper.findCount");
        System.out.println("i = " + i);
    }

    //条件查询
    @Test
    public void findById() {
        Student stu = session.selectOne("StudentMapper.findById", 2);
        System.out.println(stu);

    }

    //查询所有
    @Test
    public void findAll() {
        //4 执行对应操作
        //selectList:参数  namespace+"."+statement对象ID
        List<Student> list = session.selectList("StudentMapper.findAll");
        System.out.println(list);

    }


}
